package com.wdz.fifter;


import cn.hutool.cache.Cache;
import cn.hutool.cache.impl.LFUCache;
import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import com.wdz.annotations.ResponseResult;
import com.wdz.annotations.SecureInterception;
import com.wdz.annotations.constant.ResponseResultAnn;
import com.wdz.http.exception.NoPermissionException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Vector;

@Component
public class RequestInterceptor implements HandlerInterceptor	 {

    private static final Log log = LogFactory.get();
    //标记名称
    public final static String RESPONSE_RESULT_ANN = ResponseResultAnn.RESPONSE_RESULT_ANN;
    @Value("${server.port}")
    private String port;
    @Value("${http.ip-allow}")
    private String ip_allow;
    /**
     * 进入controller层之前拦截请求
     * @param httpServletRequest
     * @param httpServletResponse
     * @param handler
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object handler) throws Exception {
        log.info("---------------------开始进入请求地址拦截----------------------------");
        System.out.println(port);
        //String test = httpServletRequest.getHeader("test");
        System.out.println(httpServletRequest.getHeader("Host"));
        //System.out.println(httpServletRequest.getRequestURI());
        //请求的方法
        /*long a = System.currentTimeMillis();
        System.out.println(a);*/
        if(handler instanceof HandlerMethod){
            final HandlerMethod handlerMethod = (HandlerMethod) handler;
            final Class<?> clazz = handlerMethod.getBeanType();
            final Method method = handlerMethod.getMethod();
            //先判断外界有没有权限访问该接口
            String[] ip_allows = ip_allow.split(",");
            boolean isNoP = true;
            if(!"*".equals(ip_allow)) {
                for(String ip_allow:ip_allows) {
                    if((ip_allow+":"+port).equals(httpServletRequest.getHeader("Host"))) {
                        isNoP = false;
                    }
                }
                if(isNoP){
                    if (clazz.isAnnotationPresent(SecureInterception.class)) {
                        throw new NoPermissionException();
                    } else if (method.isAnnotationPresent(SecureInterception.class)) {
                        throw new NoPermissionException();
                    }
                }
            }
            if(clazz.isAnnotationPresent(ResponseResult.class)){
                //设置此请求返回体，需要包装，往下传递，在ResponseBodyAdvice接口进行判断
                httpServletRequest.setAttribute(RESPONSE_RESULT_ANN,clazz.getAnnotation(ResponseResult.class));
            }else if(method.isAnnotationPresent(ResponseResult.class)){
                //设置此请求返回体，需要包装，往下传递，在ResponseBodyAdvice接口进行判断
                httpServletRequest.setAttribute(RESPONSE_RESULT_ANN,method.getAnnotation(ResponseResult.class));
            }
        }
        /*long b = System.currentTimeMillis();
        System.out.println(b);
        System.out.println("运行时间:"+(b-a));*/
        return true;

    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
        //logger.info("--------------处理请求完成后视图渲染之前的处理操作---------------");
    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
        //logger.info("---------------视图渲染之后的操作-------------------------");
    }

	

	
}
